package cliente;

import java.util.Date;
import java.util.List;

import persistencia.DAOException;
import persistencia.DAOFactory;
import persistencia.RNException;

public class ClienteRN {

	private ClienteDAO clienteDAO = DAOFactory.getClienteDAO();
	private ClienteDAOHibernate clienteDAOHibernate = new ClienteDAOHibernate();

	public void salvar(Cliente cliente) throws RNException {
		try {
			clienteDAO.getSession().beginTransaction();
			cliente.setDataAtualizacao(new Date());
			cliente.setDataCadastro(new Date());
			clienteDAO.save(cliente);
			clienteDAO.getSession().getTransaction().commit();
		} catch (DAOException e) {
			e.printStackTrace();
			throw new RNException();
		}
	}

	public List<Cliente> listar() throws RNException {

		try {
			return clienteDAO.findAll();

		} catch (DAOException e) {
			e.printStackTrace();
			throw new RNException();
		}
	}

	public Cliente buscaPorCpfCnpj(String cpfCnpj) {
		return clienteDAOHibernate.buscaPorCpfCnpj(cpfCnpj);
	}
	
	public void deletar(Cliente cliente)throws RNException{
		try {
			clienteDAO.getSession().beginTransaction();
			clienteDAO.delete(cliente);
			clienteDAO.getSession().getTransaction().commit(); 
			
		} catch (DAOException e) {
			e.printStackTrace();
			throw new RNException(); 
		}
	}

}
